package com.company.admin.common.util;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 赵成林
 * @date 2019/2/17 13:25
 */
public class StrAndListUtil {

    public static String listToStr(List<Long> ids){
        if (CollectionUtils.isEmpty(ids)){
            return "";
        }

        StringBuilder sb = new StringBuilder("");
        for (Long id : ids){
            if (sb.length() == 0){
                sb.append(id);
            } else {
                sb.append(",").append(id);
            }
        }
        return sb.toString();
    }


    public static List<Long> strToList(String str){
        List<Long> ids = new ArrayList<>();
        if (StringUtils.isBlank(str)){
            return ids;
        }
        if (str.indexOf(",") > 0){
            String [] strs = str.split(",");
            for (String string : strs){
                ids.add(Long.parseLong(string));
            }
        } else {
            ids.add(Long.parseLong(str));
        }
        return ids;
    }
}
